<?php

class immophp_adm
{
    var $db = '';
    var $lang = '';
    var $Session = '';
    
    public function __construct($db, $lang, $session)
    {
        $this->db = $db;
        $this->lang = $lang;
        $this->Session = $session;
    }
    /**
     * @desc function generique recuperant les evenements pour un utilisateur donnee. Recuperer l id utilisateur par les var de session
     * */
    public function get_event_user_index()
    {
        global $db, $lang, $smarty;
        $Sql = $db->sql_query("
            SELECT * 
            FROM immo_event 
            WHERE uid = '". $_SESSION['iduser'] ."'
            AND 
                (start_date BETWEEN '".date('Y-m-d 00:00:00')."' AND  '".date('Y-m-d 23:59:59')."')
            AND 
                (end_date BETWEEN '".date('Y-m-d 00:00:00')."' AND  '".date('Y-m-d 23:59:59')."')
            ORDER BY start_date, end_date
            ");
        
        if($db->nb_resultat > 0)
        {

            $events = array();
            
            while($Event = $db->sql_fetcharray($Sql))
            {
                if(!$Event['type'] = immophp_adm::get_event_type($Event['type'])) $Event['type'] = $Event['type'];
                array_push($events, $Event);
            }  
            
            $smarty->assign('events', $events);
            $smarty->assign('lang', $lang);
            
            return $smarty->fetch('index_event.tpl');
        }  
        
        return false;     
    }
    
    /**
     * Recupere le type d evenment
     * */
    public function get_event_type($type)
    {
        global $db;
        
        $type_text = $db->sql_query("SELECT * FROM immo_event_type WHERE id = '". $type ."'",'OBJECT');
        if($db->nb_resultat == 1)
            return $type_text->nom;
        else return false;
        
    }
    
    /**
     * @desc verifie les parametres
     * */
    public function get_alert()
    {
        $v_alert = '';
        $path = str_replace('administration'. DIRECTORY_SEPARATOR .'','',IMMOPHP_ROOT_PATH);
        // Verification ecriture dossier conf
        if(is_writable($path.'conf'))
            $v_alert .= '<div class="error_fatal">Folder : <strong>conf</strong> is writable</div><br />';
            
        if(!is_writable($path . 'upload'))
            $v_alert .= '<div class="error_fatal">Folder : <strong>upload</strong> is not writable</div><br />';
            
        if(!is_writable($path . 'upload/agences/'))
            $v_alert .= '<div class="error_fatal">Folder : <strong>upload/agences/</strong> is not writable</div><br />';
            
        if(!is_writable($path . 'upload/annonces/'))
            $v_alert .= '<div class="error_fatal">Folder : <strong>upload/annonces/</strong> is not writable</div><br />';
            
        if(!is_writable($path . 'upload/temporaire/'))
            $v_alert .= '<div class="error_fatal">Folder : <strong>upload/temporaire/</strong> is not writable</div><br />';
            
        if(!is_writable($path . 'cache/'))
            $v_alert .= '<div class="error_warning">Folder : <strong>cache</strong> is not writable<br />Le support du cache n est pas encore operationnel.<br />Pour activer le cache renommer <strong>_cache_</strong> en <strong>cache</strong> et changer le CHMOD en 0777</div><br />';
            
        if(!is_writable($path . 'kernel/includes/mpdf/tmp/'))
            $v_alert .= '<div class="error_warning">Folder : <strong>kernel/includes/mpdf/tmp/</strong> is not writable<br />Ce dossier doit etre accessible en ecriture pour la generation de PDF.</div><br />';
            
        return $v_alert;
    }
    
    public function get_user_online()
    {
        $html_c = '';
        $i = 1;
        $t = time() - 300;
        $Query = "SELECT page, last_used FROM immo_session WHERE last_used > '". $t ."'";
        $Sql = $this->db->sql_query($Query);
        if($this->db->nb_resultat > 0 && $this->Session->check_admin())
        {
            $html_c = '<br /><br /><hr /><br /><table style="margin:auto; width:95%;"><tr><td class="thead">#</td><td class="thead">'. $this->lang['Page'] .'</td><td class="thead">'. $this->lang['Date'] .'</td></tr>';    
            while($Visiteur = $this->db->sql_fetcharray($Sql))
            {
                $html_c .= '<tr><td class="tcontenue">'. $i .'</td><td class="tcontenue">'. $Visiteur['page'] .'</td><td class="tcontenue">'. date("H:i:s", $Visiteur['last_used']) .'</td></tr>';     
                $i++;
            }
            $html_c .= '</table>';
            $this->db->sql_freeresult($Sql);
            return $html_c;
        } 
        else
            return $html_c;   
    }
    /**
     * @desc enregistre les logs de connexion
     * @param $res integer 1: pas id et mdp | 2: erreur id ou mdp | 3 ok | 4 captcha error
     * @return void
     * */
    public function log_connexion($res)
    {
        $QA = array(
        'identifiant'   =>  trim(htmlentities($_POST['identifiant'])),
        'date_c'    =>  time(),
        'ip'    =>  $_SERVER['REMOTE_ADDR'],
        'resultat'  =>  $res
        );
        $this->db->sql_query(CreateQueryInsert($QA, 'immo_log_connexion_admin'));
    }

}